package com.app.twth.ui.checkin;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.app.twth.R;
import com.app.twth.bean.PhotoBean;
import com.app.twth.databinding.DialogShowPhotoBinding;
import com.app.twth.databinding.ItemCheckinPhotoListBinding;
import com.app.twth.interfaces.MyOnClickListener;
import com.app.twth.interfaces.MyStringOnClickListener;
import com.app.twth.utils.MyDialogUtil;
import com.app.twth.utils.Utils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;


public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.MyViewHolder> {
    private Context mContext;
    private List<PhotoBean> images;
    private int count;
    private AlertDialog dialogPhoto;
    private LayoutInflater mInflater;
    private MyOnClickListener locationListener;
    private MyStringOnClickListener deleteListener;

    public PhotoAdapter(Context mContext, List<PhotoBean> images, int count, MyOnClickListener locationListener, MyStringOnClickListener deleteListener) {
        this.mContext = mContext;
        this.images = images;
        this.count = count;
        this.mInflater = LayoutInflater.from(mContext);
        this.locationListener = locationListener;
        this.deleteListener = deleteListener;
    }

    public void setData(List<PhotoBean> images) {
        this.images = images;
        notifyDataSetChanged();
    }

    @Override
    public PhotoAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new PhotoAdapter.MyViewHolder(ItemCheckinPhotoListBinding.inflate(mInflater, parent, false));
    }

    @Override
    public void onBindViewHolder(PhotoAdapter.MyViewHolder holder, int position) {
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        if (images == null) {
            return 0;
        }
        if (count < 1) {
            return images.size();
        }
        return count < images.size() ? count : images.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ItemCheckinPhotoListBinding itemBinding;

        public MyViewHolder(ItemCheckinPhotoListBinding itemBinding) {
            super(itemBinding.getRoot());
            this.itemBinding = itemBinding;
        }

        public void setData(int position) {
            itemView.setTag(position);

            PhotoBean photoBean = images.get(position);

            if (Utils.isEmpty(photoBean.getPathImg())) {
                itemBinding.iv.setImageResource(R.drawable.ic_add_image);
                itemBinding.ivDelete.setVisibility(View.GONE);
            } else {
                setShow(photoBean.getPathImg(), itemBinding.iv, itemBinding.ivDelete);
            }
            itemBinding.ivDelete.setOnClickListener(v -> {
                if (!Utils.isEmpty(photoBean.getPathImg())) {
                    deleteListener.OnClick(photoBean.getUrlImg());
                }
                images.remove(position);
                boolean isAdd = false;
                for (PhotoBean photoBean1 : images) {
                    if (Utils.isEmpty(photoBean1.getPathImg())) {
                        isAdd = true;
                    }
                }
                if (!isAdd) {
                    PhotoBean photoBean1 = new PhotoBean();
                    photoBean1.setUrlImg("");
                    photoBean1.setPathImg("");
                    images.add(photoBean1);
                }
                notifyDataSetChanged();
            });

            itemBinding.getRoot().setOnClickListener(v -> {
                if (Utils.isEmpty(photoBean.getPathImg())) {
                    locationListener.OnClick(position);
                } else {
                    showPhotoDialog(photoBean.getPathImg());
                }
            });
        }
    }

    private void setShow(String pathImage, ImageView iv_show, ImageView iv_delete) {
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.picture_icon_editor)//图片加载出来前，显示的图片
                .fallback(R.mipmap.picture_icon_editor) //url为空的时候,显示的图片
                .error(R.mipmap.picture_icon_warning);//图片加载失败后，显示的图片

        Glide.with(mContext)
                .load(pathImage)
                .apply(options)
                .into(iv_show);
        iv_delete.setVisibility(View.VISIBLE);
    }

    private void showPhotoDialog(String path) {
        dialogPhoto = MyDialogUtil.getDialog(mContext, initPhotoDialogView(path), Gravity.CENTER);
        dialogPhoto.setCanceledOnTouchOutside(true);
        dialogPhoto.show();
    }

    private View initPhotoDialogView(String path) {
        DialogShowPhotoBinding dialogBinding = DialogShowPhotoBinding.inflate(LayoutInflater.from(mContext));
        dialogBinding.ivImg.setOnViewTapListener((v, x, y) -> {
            if (dialogPhoto != null) {
                dialogPhoto.dismiss();
            }
        });
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.picture_icon_editor)//图片加载出来前，显示的图片
                .fallback(R.mipmap.picture_icon_editor) //url为空的时候,显示的图片
                .error(R.mipmap.picture_icon_warning);//图片加载失败后，显示的图片
        Glide.with(mContext).load(path).apply(options).into(dialogBinding.ivImg);
        return dialogBinding.getRoot();
    }

}
